const https = require('https');

let limit24coinarrr=[ //市值前24币种
    {symbol:'BTC',data:null},
    {symbol:'ETH',data:null},
    {symbol:'XRP',data:null},
    {symbol:'BCH',data:null},
    {symbol:'EOS',data:null},
    {symbol:'LTC',data:null},
    {symbol:'XLM',data:null},
    {symbol:'ADA',data:null},
    {symbol:'MIOTA',data:null},
    {symbol:'TRX',data:null},
    {symbol:'NEO',data:null},
    {symbol:'USDT',data:null},
    {symbol:'DASH',data:null},
    {symbol:'XMR',data:null},
    {symbol:'XEM',data:null},
    {symbol:'BNB',data:null},
    {symbol:'VEN',data:null},
    {symbol:'ETC',data:null},
    {symbol:'QTUM',data:null},
    {symbol:'OMG',data:null},
    {symbol:'ICX',data:null},
    {symbol:'BCN',data:null},
    {symbol:'ZEC',data:null},
    {symbol:'ONT',data:null}
]

const coinlimt24 = async () => { //默认获取24条币种信息
    const result = new Promise((resolve, reject) => {
        let req=https.get('https://api.coinmarketcap.com/v2/ticker/?limit=24', (res) => {
            let data = ''
            res.on('data', (chunk) => {
                data += chunk
            });
            res.on('end', () => {
                let obj = JSON.parse(data)
                let resultarr=[]
                if(obj.data){
                    limit24coinarrr.forEach(ele => {
                        for(let key in obj.data){
                            if(ele.symbol==obj.data[key].symbol){
                                ele.data=obj.data[key]
                            }
                        }
                    })
                    resolve(limit24coinarrr)
                }else{
                    resolve(resultarr)
                }
            })
        })
        req.on('error', (e) => {
            reject(e)
        })
        //写入数据到请求主体
        req.end()
    })
    return result
}

module.exports={
    coinlimt24,
}